/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import logging.CommonsLogging;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import vo.rb_emp_salary_structure;
import vo.rb_expence;

/**
 *
 * @author Shuhail
 */
public class rb_emp_sal_structure_persist {
    
    Session sess = null;
    Transaction tx = null;
    Logger logger = CommonsLogging.getInstance().getLogger();
    private static rb_emp_sal_structure_persist getInstance() {
        
        return new rb_emp_sal_structure_persist();
    }
    
    public rb_emp_sal_structure_persist() {
        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        sess = sf.openSession();
        tx = sess.beginTransaction();
    }
    
    public List getAllEmployeeSAlary() {
        List list = null;
        Query query = sess.getNamedQuery("getAllEmployeeSalary");
        list = query.list();
        return list;
    }
    
    public boolean updateAllEmployeeSalary(List list) {
       
        tx.begin();
        Iterator it =list.iterator();
        while(it.hasNext()){
            
        rb_emp_salary_structure emp = (rb_emp_salary_structure)it.next();
         
        rb_emp_salary_structure empVO = (rb_emp_salary_structure)sess.get(rb_emp_salary_structure.class, emp.getEmployee_id());
        if(empVO==null){
            if(logger.isDebugEnabled())
                logger.debug("No data exists.. So inserting fresh record to DB");
           sess.save(emp);
        }
        else {
            if(logger.isDebugEnabled()) 
                logger.info("Updating As :::"+empVO.getEmployee_id()+" already exists");
            empVO.setBasic_pay(emp.getBasic_pay());
            empVO.setDa(emp.getDa());
            empVO.setNet_salary(emp.getNet_salary());
            empVO.setPhone(emp.getPhone());
            empVO.setTa(emp.getTa());
          sess.update(empVO);  
        }
        }
        tx.commit();
        return true;
    }
    public boolean insertEmployeeSalary(rb_emp_salary_structure emp) {
        
        tx.begin();
        sess.save(emp);
        tx.commit();
        return true;
    }
    
    public static void main(String[] args) {
       List list = new rb_emp_sal_structure_persist().getAllEmployeeSAlary();
       Iterator it = list.iterator();
       while(it.hasNext()) {
           rb_emp_salary_structure ex = (rb_emp_salary_structure)it.next();
           System.out.println(ex.getEmployee_id()+"+====+"+ex.getBasic_pay()+"====="+ex.getTa()+"==="+ex.getDa());
       }
        
    }
   
}
